package uk.ac.cam.tcs40.sidnet.formal;

import java.util.LinkedList;
import java.util.List;

import uk.ac.cam.tcs40.sidnet.Hall;
import uk.ac.cam.tcs40.sidnet.R;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class BookingFragment extends Fragment {

	private Formal m_Formal;

	private static final int PICK_CONTACT = 1001;

	private CheckBox m_Attending;
	private Spinner m_Dietary;
	private EditText m_Notes;
	private ListView m_GuestList;
	private GuestAdapter m_GuestListAdapter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Bundle extras = getActivity().getIntent().getExtras();

		String id;
		if (extras != null) {
			id = extras.getString("id");
			if (id != null) {
				m_Formal = Hall.getInstance().getFormal(id);
				new DownloadFormalBookingTask().execute();
			}
		}
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

		super.onCreateView(inflater, container, savedInstanceState);

		final View view = inflater.inflate(R.layout.activity_formal_booking, container, false);

		final TextView dateTextView = (TextView) view.findViewById(R.id.date);
		dateTextView.setText(m_Formal.getDateString());

		m_Attending = (CheckBox) view.findViewById(R.id.attending);
		m_Attending.setChecked(m_Formal.getUser().isAttending());
		m_Attending.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView, final boolean isChecked) {
				new Thread() {
					@Override
					public void run() {
						m_Formal.getUser().setAttending(isChecked);
					}
				}.start();
			}
		});

		m_Dietary = (Spinner) view.findViewById(R.id.dietary_requirements);
		m_Dietary.setAdapter(new ArrayAdapter<Dietary>(getActivity(), android.R.layout.simple_list_item_1, Dietary.values()));
		m_Dietary.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View itemView, int position, long id) {

				m_Formal.getUser().setDietary((Dietary)parent.getItemAtPosition(position));

				if ((Dietary)parent.getItemAtPosition(position) == Dietary.OTHER) {
					view.findViewById(R.id.notes).setVisibility(View.VISIBLE);
				} else {
					view.findViewById(R.id.notes).setVisibility(View.INVISIBLE);
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub

			}
		});

		m_Notes = (EditText) view.findViewById(R.id.notes);

		m_GuestList = (ListView) view.findViewById(R.id.guest_list);
		m_GuestListAdapter = new GuestAdapter(getActivity(), m_Formal.getGuests());
		m_GuestListAdapter.setFormal(m_Formal);
		m_GuestList.setAdapter(m_GuestListAdapter);

		setHasOptionsMenu(true);

		return view;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {

		case R.id.action_add_guest:
			m_Formal.addGuest(new Guest(null, Dietary.NONE, null));
			m_GuestListAdapter.notifyDataSetChanged();
		}

		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == PICK_CONTACT) {
			if (data != null) {

				Uri uri = data.getData();

				if (uri != null) {
					Cursor c = null;
					try {
						c = getActivity().getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, new String[]{ 
								ContactsContract.Contacts.DISPLAY_NAME },
								null, null, null);

						if (c != null && c.moveToFirst()) {
							String name = c.getString(0);
							Log.i("name = ", name);
						}
					} finally {
						if (c != null) {
							c.close();
						}
					}
				}
			}
		}
		super.onActivityResult(requestCode, resultCode, data);
	}

	private class DownloadFormalBookingTask extends AsyncTask<Void, Void, Void> {
		protected void onPreExecute() {
		}

		protected Void doInBackground(Void... v) {
			if (m_Formal.getUser().isAttending())
				Hall.getInstance().getFormalBookingDetails(m_Formal);
			return null;
		}

		protected void onProgressUpdate(Void... progress) {
		}

		protected void onPostExecute(Void result) {
			m_Dietary.setSelection(m_Formal.getUser().getDietary().ordinal());
			m_Notes.setText(m_Formal.getUser().getNotes());

			m_GuestListAdapter.notifyDataSetChanged();
		}
	}
}
